import 'package:lib_bt/BleManager.dart';
import 'package:lib_bt/bean/BleBeanInterface.dart';
import 'package:lib_bt/listener/OnBleConnectListener.dart';
import 'package:lib_bt/listener/OnBleScanListener.dart';
import 'package:lib_util/SPUtils.dart';
import 'base/LedBleBeanInterface.dart';

///自动连接工具类
class AutoConnectUtil implements OnBleScanListener, OnBleConnectListener {
  static const String AUTO_CONNECTT_KEY = "auto_connectt_key";
  static const String LAST_CONNECTT_KEY = "last_connectt_key";

  //单例
  static AutoConnectUtil? _instance;

  static AutoConnectUtil get instance => _instance ??= AutoConnectUtil._();

  //最后一个连接的蓝牙设备
  BleBeanInterface? lastConnectBleBean;

  AutoConnectUtil._() {
    //getLastConnectBleBean();
    BleManager.instance.addScanListener(this);
    BleManager.instance.addConnectListener(this);
  }

  //自动连接
  void autoConnect() {
    var autoConnectBleName = SPUtils.getAsString(AUTO_CONNECTT_KEY);
    if (autoConnectBleName.isNotEmpty) {
      BleManager.instance.startScan();
    }
  }

  @override
  void onScanResult(result) {
    if (BleManager.instance.isConnecting) {
      return;
    }
    String address = result["address"];
    var bleBean = BleManager.instance.getBleBean<LedBleBeanInterface>(address);
    if (bleBean == null || !bleBean.isAutoConnect) {
      return;
    }
    var autoConnectBleName = SPUtils.getAsString(AUTO_CONNECTT_KEY);
    if (autoConnectBleName.isNotEmpty && autoConnectBleName == bleBean.name) {

      var bleList = BleManager.instance.getConnectBleBeans();
      //跟已经连接的设备类型不一致的不自动连
      if (bleList.isNotEmpty) {
        var lastBle = BleManager.instance.getBleBean<LedBleBeanInterface>(bleList.last.address);
        if (lastBle != null) {
          if (!lastBle.typeIsSame(bleBean)) {
            return;
          }
        }
      }
      BleManager.instance.connect(address);
    }
  }

  @override
  void onStartScan() {}

  @override
  void onStopScan() {}

  @override
  void onConnected(String address) {
    var bleBean = BleManager.instance.getBleBean<LedBleBeanInterface>(address);
    if (bleBean == null || !bleBean.isAutoConnect) {
      return;
    }

    //保存自动连接的蓝牙名称
    saveAutoConnect(bleBean.name);
    saveLastConnect(bleBean);
  }

  BleBeanInterface getLastConnectBleBean() {
    var bleList = BleManager.instance.getConnectBleBeans();
    if (bleList.isNotEmpty) {
      return bleList.last;
    }

    if (lastConnectBleBean == null) {
      var autoConnectBleName = SPUtils.getAsString(LAST_CONNECTT_KEY);
      if (autoConnectBleName.isNotEmpty) {
        lastConnectBleBean = BleBeanInterface.fromJson(autoConnectBleName);
        lastConnectBleBean!.isConnect = false;
      }
    }

    //伪造一个默认的蓝牙设备
    if (lastConnectBleBean == null) {
      lastConnectBleBean = BleBeanInterface();
      lastConnectBleBean!.bytes = [
        1,
        84,
        89,
        0,
        1,
        0,
        12,
        0,
        48,
        1,
        0,
        7,
        0,
        1,
        0,
        1,
        0,
        0,
        0,
        0,
        13,
        9,
        108,
        101,
        100,
        115,
        104,
        111,
        119,
        45,
        69,
        49,
        50,
        51,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ];
      lastConnectBleBean!.address = "00:00:00:00:00:00";
    }
    return lastConnectBleBean!;
  }

  //保存自动连接的蓝牙名称
  void saveAutoConnect(String bleName) {
    //保存自动连接的蓝牙名称
    SPUtils.setAsString(AUTO_CONNECTT_KEY, bleName);
  }

  //保存最后一个连接的设备的信息
  void saveLastConnect(BleBeanInterface bleBean) {
    lastConnectBleBean = bleBean;
    SPUtils.setAsString(LAST_CONNECTT_KEY, bleBean.toJson());
  }

  @override
  void onDisconnected(String address) {}

  @override
  void onMtuChanged(String address, int mtu) {}

  @override
  void onStartConnect(String address) {}
}
